

libname AZ_INP "D:\Data\Hospital Inpatient Records\Hospital Arizona (1990-2009)";
libname AHA "C:\Data\AHA Survey";

%let keep_v1 = AGE  DX1 PAY1 DSHOSPID;

data AZ_INP.Az_sidc_1995_core_ext;
  merge AZ_INP.Az_sidc_1995_core AZ_INP.Az_sidc_1995_spec (keep = SEQ_SID ADATE DOB);
  by SEQ_SID;
run;
data AZ_INP.Az_sidc_1996_core_ext;
  merge AZ_INP.Az_sidc_1996_core AZ_INP.Az_sidc_1996_spec (keep = SEQ_SID ADATE DOB);
  by SEQ_SID;
run;
data AZ_INP.Az_sidc_1997_core_ext;
  merge AZ_INP.Az_sidc_1997_core AZ_INP.Az_sidc_1997_spec (keep = SEQ_SID ADATE DOB);
  by SEQ_SID;
run;


%macro pull_az(lib,file1,file2,file3,keep_v2,keep_v3,keep_v4,keep_v5,keep_v6,keep_v7,ln);
proc freq data = &lib..&file1.;
   tables   &keep_v5.;
   title "&File1. gender";
   where age in (22,23);
run;
data junk1 (drop = &keep_v5.);
   format state $2. file $22.;
   set &lib..&file1. (keep =  &keep_v1. &keep_v2. &keep_v3. &keep_v4. &keep_v5. &keep_v6. &keep_v7.);
   file = "&file1.";
   state = substr("&lib.",1,2);
   if substr("&file1.",9,4) in (1995,1996,1997) then do;
      months_23 = intck('month',mdy(month(dob),14,year(dob)+23),mdy(month(ADATE),15,year(ADATE)));
	  ad_date = mdy(month(ADATE),15,year(ADATE));;
   end; 
   if substr("&file1.",9,4) not in (1995,1996,1997) then do;
      months_23 = intck('month',mdy(bmonth,14,byear+23),mdy(amonth,15,ayear));
	  ad_date = mdy(amonth,15,ayear);
   end;
   if months_23 = . then bad_date = 1; else bad_date = 0;
   from_ED = 0;
   if asource = "1" then from_ED = 1; 
   if &keep_v7. = "7" then from_ED = 1;
   if &keep_v6. = 20 then died = 1; else died = 0;
   if substr(DX1,1,2) in ('63','64','65','66','67') then preg_related = 1; else preg_related = 0;
   if (months_23 >= -40 and months_23 <= 40) or bad_date = 1;
   male = 0;
   if substr("&file1.",9,4) <= 1997 and &keep_v5. = 1 then male = 1; 
   if substr("&file1.",9,4) > 1997 and &keep_v5. = 0 then male = 1; 
   if substr("&file1.",9,4) <= 1997 and missing(&keep_v5.) then male = .; 
   if substr("&file1.",9,4) > 1997 and missing(&keep_v5.) then male = .; 
   count = 1;
run;
proc freq data = junk1;
   tables count;
   where  bad_date = 1 and age in (22,23);
   title "&File1. Missing Exact Age";
run;

proc sort data = junk1; by DSHOSPID; run;
proc sort data = &lib..&file2. (keep = DSHOSPID AHAID) out = junk2;  by AHAID; run;
data junk3;
   merge junk2 (in = ina) AHA.&file3.;
   by AHAID;
   if ina = 1; 
run;
proc sort data = junk3;   by DSHOSPID; run;
data &file1. (drop = DSHOSPID &keep_v4.);
   merge junk1 (in = ina) junk3;
   by DSHOSPID; 
   if ina = 1;
	if bad_date = 1 then delete;
	if ad_date < mdy(1,1,1990) then delete;
	visit = 1;
   if cntrl in (12,13,14,15,16) then cntrl_public = 1; else cntrl_public = 0;
   if cntrl in (21,23) then cntrl_non_profit = 1; else cntrl_non_profit = 0;
   if cntrl in (31,32,33) then cntrl_for_profit = 1; else cntrl_for_profit = 0;
   if cntrl not in (12,13,14,15,16,21,23,31,32,33) then cntrl_other = 1; else cntrl_other = 0;
   if pay1 = 2 then medicaid = 1; else medicaid = 0;
   if pay1 = 3 then private = 1; else private = 0;
   if pay1 = 4 then self_pay = 1; else self_pay = 0;
   if pay1 not in (2,3,4) then other_ins = 1; else other_ins = 0;
run;
proc datasets;
   delete junk1 junk2 junk3;
run;
%mend;

%pull_az(AZ_INP,az_sidc_1990_core,az_sidc_1990_ahal,aha_extract1990,DX1,DX2-DX5,bmonth byear amonth ayear asource,sex,DISP,ayear,5);
%pull_az(AZ_INP,az_sidc_1991_core,az_sidc_1991_ahal,aha_extract1991,DX1,DX2-DX5,bmonth byear amonth ayear asource,sex,DISP,ayear,5);
%pull_az(AZ_INP,az_sidc_1992_core,az_sidc_1992_ahal,aha_extract1992,DX1,DX2-DX5,bmonth byear amonth ayear asource,sex,DISP,ayear,5);
%pull_az(AZ_INP,az_sidc_1993_core,az_sidc_1993_ahal,aha_extract1993,DX1,DX2-DX5,bmonth byear amonth ayear asource,sex,DISP,ayear,5);
%pull_az(AZ_INP,az_sidc_1994_core,az_sidc_1994_ahal,aha_extract1994,DX1,DX2-DX5,bmonth byear amonth ayear asource,sex,DISP,ayear,5);
%pull_az(AZ_INP,Az_sidc_1995_core_ext,az_sidc_1995_ahal,aha_extract1995,DX1,DX2-DX11,DOB ADATE asource,sex,DISP,DOB,11);
%pull_az(AZ_INP,Az_sidc_1996_core_ext,az_sidc_1996_ahal,aha_extract1996,DX1,DX2-DX11,DOB ADATE asource,sex,DISP,DOB,11);
%pull_az(AZ_INP,Az_sidc_1997_core_ext,az_sidc_1997_ahal,aha_extract1997,DX1,DX2-DX11,DOB ADATE asource,sex,DISP,DOB,11);
%pull_az(AZ_INP,az_sidc_1998_core,az_sidc_1998_ahal,aha_extract1998,DX1,DX2-DX11,bmonth byear amonth ayear asource,female,DISPUNIFORM,ayear,11);
%pull_az(AZ_INP,az_sidc_1999_core,az_sidc_1999_ahal,aha_extract1999,DX1,DX2-DX11,bmonth byear amonth ayear asource,female,DISPUNIFORM,ayear,11);
%pull_az(AZ_INP,az_sidc_2000_core,az_sidc_2000_ahal,aha_extract2000,DX1,DX2-DX11,bmonth byear amonth ayear asource,female,DISPUNIFORM,ayear,11);
%pull_az(AZ_INP,az_sidc_2001_core,az_sidc_2001_ahal,aha_extract2001,DX1,DX2-DX11,bmonth byear amonth ayear asource,female,DISPUNIFORM,ayear,11);
%pull_az(AZ_INP,az_sidc_2002_core,az_sidc_2002_ahal,aha_extract2002,DX1,DX2-DX11,bmonth byear amonth ayear asource,female,DISPUNIFORM,ayear,11);
%pull_az(AZ_INP,az_sidc_2003_core,az_sidc_2003_ahal,aha_extract2003,ECODE1,DX2-DX9,bmonth byear amonth ayear asource,female,DISPUNIFORM,ayear,9);
%pull_az(AZ_INP,az_sidc_2004_core,az_sidc_2004_ahal,aha_extract2004,ECODE1,DX2-DX9,bmonth byear amonth ayear asource,female,DISPUNIFORM,ayear,9);
%pull_az(AZ_INP,az_sidc_2005_core,az_sidc_2005_ahal,aha_extract2005,ECODE1,DX2-DX9,bmonth byear amonth ayear asource,female,DISPUNIFORM,ayear,9);
%pull_az(AZ_INP,az_sidc_2006_core,az_sidc_2006_ahal,aha_extract2006,ECODE1,DX2-DX9,bmonth byear amonth ayear asource,female,DISPUNIFORM,ayear,9);
%pull_az(AZ_INP,az_sidc_2007_core,az_sidc_2007_ahal,aha_extract2007,ECODE1,DX2-DX9,bmonth byear amonth ayear asource,female,DISPUNIFORM,PointOfOriginUB04,9);
%pull_az(AZ_INP,az_sidc_2008_core,az_sidc_2008_ahal,aha_extract2008,ECODE1,DX2-DX25,bmonth byear amonth ayear,female,DISPUNIFORM,PointOfOriginUB04,25);
%pull_az(AZ_INP,az_sidc_2009_core,az_sidc_2009_ahal,aha_extract2009,ECODE1,DX2-DX25,bmonth byear amonth ayear,female,DISPUNIFORM,PointOfOriginUB04,25);

data Arizona_inp;
   format ad_date date.;
   set az_sidc_1990_core az_sidc_1991_core az_sidc_1992_core az_sidc_1993_core az_sidc_1994_core 
       Az_sidc_1995_core_ext Az_sidc_1996_core_ext az_sidc_1997_core_ext az_sidc_1998_core az_sidc_1999_core 
       az_sidc_2000_core az_sidc_2001_core az_sidc_2002_core az_sidc_2003_core az_sidc_2004_core 
       az_sidc_2005_core az_sidc_2006_core az_sidc_2007_core az_sidc_2008_core az_sidc_2009_core;
run;

%macro stab(i_file,o_file);
proc means data = &i_file. noprint;
   var visit male  preg_related from_ED medicaid private self_pay other_ins cntrl_public cntrl_non_profit cntrl_for_profit cntrl_other;
   output out = junk1 sum = visit male  preg_related from_ED medicaid private self_pay other_ins cntrl_public cntrl_non_profit cntrl_for_profit cntrl_other;
   class ad_date;
   where months_23 >= -12 and months_23 <=12;
run;
data junk2;
   format ad_date date.;
   set junk1;
   if _type_ = 0 then delete;
run; 
PROC EXPORT DATA= junk2
            OUTFILE= "C:\Research\Insurance 23\3. Code up from Scratch\&o_File..csv"
            DBMS=CSV REPLACE;
RUN;
proc datasets;
   delete junk1 junk2;
run;
%mend;

%stab(Arizona_inp,P05 Arizona Inpatient);


%macro pull_v(suf,a_where);
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk1 (rename=(_FREQ_ = &st._&suf.)) mean =  &st._medi_&suf. &st._priv_&suf. &st._self_&suf. &st._o_ins_&suf.;
   class months_23;
   where state = "&st." &a_where.;
run;
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk2 (rename=(_FREQ_ = &st._&suf._ED)) mean =  &st._medi_&suf._ED &st._priv_&suf._ED &st._self_&suf._ED &st._o_ins_&suf._ED;
   class months_23;
   where state = "&st." and from_ED = 1 &a_where.;
run;
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk3 (rename=(_FREQ_ = &st._&suf._NED)) mean =  &st._medi_&suf._NED &st._priv_&suf._NED &st._self_&suf._NED &st._o_ins_&suf._NED;
   class months_23;
   where state = "&st." and from_ED = 0 &a_where.;
run;
data &suf.;
   merge junk1 junk2 junk3;
   by months_23;
run;
%mend;

%macro p_state(st,i_File);
   %pull_v(all,);
   %pull_v(all_pub,and cntrl_public = 1);
   %pull_v(all_non_prof,and cntrl_non_profit = 1);
   %pull_v(all_for_prof,and cntrl_for_profit = 1);
   %pull_v(all_other,and cntrl_other = 1);
   %pull_v(all_np,and preg_related = 0);
   %pull_v(all_np_pub,and preg_related = 0 and cntrl_public = 1);
   %pull_v(all_np_non_prof,and preg_related = 0 and cntrl_non_profit = 1);
   %pull_v(all_np_for_prof,and preg_related = 0 and cntrl_for_profit = 1);
   %pull_v(all_np_other,and preg_related = 0 and cntrl_other = 1);
   %pull_v(female,and male = 0);
   %pull_v(female_pub,and male = 0 and cntrl_public = 1);
   %pull_v(female_non_prof,and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_for_prof,and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_other,and male = 0 and cntrl_other = 1);
   %pull_v(female_np,and preg_related = 0 and male = 0);
   %pull_v(female_np_pub,and preg_related = 0 and male = 0 and cntrl_public = 1);
   %pull_v(female_np_non_prof,and preg_related = 0 and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_np_for_prof,and preg_related = 0 and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_np_other,and preg_related = 0 and male = 0 and cntrl_other = 1);
   %pull_v(female_p,and preg_related = 1 and male = 0);
   %pull_v(female_p_pub,and preg_related = 1 and male = 0 and cntrl_public = 1);
   %pull_v(female_p_non_prof,and preg_related = 1 and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_p_for_prof,and preg_related = 1 and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_p_other,and preg_related = 1 and male = 0 and cntrl_other = 1);
   %pull_v(male,and male = 1);
   %pull_v(male_pub,and male = 1 and cntrl_public = 1);
   %pull_v(male_non_prof,and male = 1 and cntrl_non_profit = 1);
   %pull_v(male_for_prof,and male = 1 and cntrl_for_profit = 1);
   %pull_v(male_other,and male = 1 and cntrl_other = 1);
   data inter.ins_23_reprod_&ST. (drop = _type_ );
      merge all 		all_pub 		all_non_prof 		all_for_prof 		all_other
       	 	all_np 		all_np_pub 		all_np_non_prof 	all_np_for_prof 	all_np_other
         	female 		female_pub 		female_non_prof 	female_for_prof 	female_other
   		 	female_np 	female_np_pub 	female_np_non_prof 	female_np_for_prof 	female_np_other
   		 	female_p 	female_p_pub 	female_p_non_prof 	female_p_for_prof 	female_p_other
   		 	male 		male_pub 		male_non_prof 		male_for_prof 		male_other;
      by months_23;
      if months_23 = . then delete;
      merge_v = 1;
   run;
%mend;

%p_state(AZ,Arizona_inp);



proc datasets;
   delete all 		all_pub 		all_non_prof 		all_for_prof 		all_other
       	 	all_np 		all_np_pub 		all_np_non_prof 	all_np_for_prof 	all_np_other
         	female 		female_pub 		female_non_prof 	female_for_prof 	female_other
   		 	female_np 	female_np_pub 	female_np_non_prof 	female_np_for_prof 	female_np_other
   		 	female_p 	female_p_pub 	female_p_non_prof 	female_p_for_prof 	female_p_other
   		 	male 		male_pub 		male_non_prof 		male_for_prof 		male_other

       az_sidc_1990_core az_sidc_1991_core az_sidc_1992_core az_sidc_1993_core az_sidc_1994_core 
       Az_sidc_1995_core_ext Az_sidc_1996_core_ext az_sidc_1997_core_ext az_sidc_1998_core az_sidc_1999_core 
       az_sidc_2000_core az_sidc_2001_core az_sidc_2002_core az_sidc_2003_core az_sidc_2004_core 
       az_sidc_2005_core az_sidc_2006_core az_sidc_2007_core az_sidc_2008_core az_sidc_2009_core;
run;
